package com.xpec.canaan.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class FileOutputRecorder {
	
	public FileOutputRecorder(String writePath)
	{
		m_writePath = writePath;
	}
	
	/**
	 * write information into file
	 * @param info
	 */
	public void writeIntoFile(String info)
	{
		synchronized(m_writer)
		{
			try{
				m_writer.write(info);
			}
			catch(IOException exp)
			{
				System.err.println(exp.getMessage());
			}
		}
	}
	
	/**
	 * close file
	 * 
	 */
	public void closeRecorder()
	{
		synchronized(m_writer)
		{
			try{
				m_writer.flush();
				m_writer.close();
			}
			catch(IOException exp)
			{
				System.err.println(exp.getMessage());
			}
		}
	}
	
	/**
	 * 
	 */
	public boolean createOutFile()
	{
		File output = new File(m_writePath);
		if( output.exists() )
		{
			output.delete();
		}
		
		try
		{
			m_writer = new OutputStreamWriter( new FileOutputStream(m_writePath) );
			return true;
		}
		catch(FileNotFoundException except)
		{
			System.err.println(except.getMessage());
			return false;
		}
	}
	
	private String m_writePath;
	private OutputStreamWriter m_writer;
}
